/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components.TabControl = {
	Init:function()
	{
		if (!this.P.SelectedClass)
		{
			this.P.SelectedClass="TabItemSelected";
		}
		if (this.P.Align == undefined)
		{
			this.SetAlign('top');
		}
		this.UpdateHeader();
		
		this.C.header.bind('click',this,this.eventClick);
		return jsForms.Components.Component.Init.call(this);
	},
	eventClick: function(e){
		if (!e.data) {
			return;
		}
		var el = e.target;	
		do
			{
				var obj = jsForms.GetObject(el);
				if (obj) {
					switch (obj._Type) {
						case 'TabHeader':
							e.data.Select(obj.GetIndex());
							return;
						case 'TabControl':
							return;
						default:
							el = el.parentNode;
							break;
					}
				}
				else{
					el = el.parentNode;
				}
			}while(el != undefined);	
	},
	Select: function(index)
	{
		if (this.P.SelectedIndex != index) {
			if (this.P.SelectedTab)
			{
				var eventArg = {isCancel:false};
				this.P.SelectedTab.Deselect(eventArg);
				if (eventArg.isCancel == true)
				{
					return;
				}
				this.P.SelectedTab.hide();
			}
			if (this.P.SelectedTabHeader)
			{
				this.P.SelectedTabHeader.removeClass(this.P.SelectedClass);
			}
			this.P.SelectedIndex = index;
			if (this.P.tabs) {
				var tab = this.P.tabs[index];
				this.P.SelectedTab = tab;
				this.P.SelectedTabHeader = this.P.tabsHeader[index];
				if (tab)
				{
					tab.Select().show();
				}
			}
			this._ExecEvent('onChange',this.P.SelectedTab);
			if (this.P.SelectedTabHeader)
			{
				this.P.SelectedTabHeader.addClass(this.P.SelectedClass);
			}
		}
		return this;
	},
	AppendTabToHeader:function(tab)
	{
		if (!tab || tab._Type!='TabItem')
		{
			return;
		}
		tab.hide();
		var obj = jsForms.CreateComponent('TabHeader');
		obj.SetTitle(tab.GetTitle()).SetIndex(this.P.tabs.length).hide().appendTo(this.C.header).Init().show();
		this.P.tabsHeader.push(obj);
		this.P.tabs.push(tab);
		return this;
	},
	AppendTab:function(tab)
	{
		this.AppendTabToHeader(tab);
		this.C.content.append(tab);
		return this;
	},
	UpdateHeader: function(){
		this.C.header.empty();
		if (this.P.tabs != undefined)
		{
			delete (this.P.tabs);
		}
		if (this.P.tabsHeader != undefined)
		{
			delete (this.P.tabsHeader);
		}
		this.P.tabs=[];
		this.P.tabsHeader=[];
		var items = this.C.content[0].childNodes;
		for(var i=0 ; i < items.length ; i++)
		{
			var tab =jsForms.GetObject(items[i]);
			this.AppendTabToHeader(tab);
		};
		return this;
	},
	SetAlign: function(value)
	{
		if (this.P.Align != value) {
			switch (value) {
				case 'bottom':{
					this.C.content.after(this.C.header).addClass('TabControlContentBottom').removeClass('TabControlContentTop');
					this.C.header.addClass('TabControlHeaderBottom').removeClass('TabControlHeaderTop');
					break;
				}
				default://case 'top':
					{
						this.C.content.before(this.C.header).addClass('TabControlContentTop').removeClass('TabControlContentBottom');
						this.C.header.addClass('TabControlHeaderTop').removeClass('TabControlHeaderBottom');
						break;
					}
			}
			this.P.Align = value;
		}
		return this;
	}
};
jsForms.Components.TabHeader = {
	SetIndex:function(value)
	{
		this.P.Index = value;
		return this;
	},
	GetIndex:function()
	{
		return this.P.Index;
	},
	SetTitle:function(value)
	{
		if (this.P.Title != value) {
			this.P.Title = value;
			this.C.tabName[0].innerHTML = value;
		}
		return this;
	},
	GetTitle:function()
	{
		return this.P.Title;
	}
};
jsForms.Components.TabItem = {
	Select:function(eventArg)
	{
		this._ExecEvent('onSelect',eventArg);
		return this;
	},
	Deselect:function(eventArg)
	{
		this._ExecEvent('onDeselect',eventArg);
		return this;
	},
	ClearContent:function()
	{
		this.C.content[0].innerHTML = '';
		return this;
	},
	AppendContent:function(obj)
	{
		this.C.content.append(obj);
		return this;
	},
	SetTitle:function(value)
	{
		this.P.Title = value;
		return this;
	},
	GetTitle:function()
	{
		return this.P.Title;
	}
};
jsForms.CreateCompileInfoByObject(
{
	Name: 'TabControl',
	Properties: {
		SelectedClass: {
			Type: 'String',
			Access: 'direct'
		},
		Align:{}
	},
	Events:{
		onChange:{}
	},
	Contents:{
		content:{}
	}
});
jsForms.CreateCompileInfoByObject(
{
	Name: 'TabItem',
	Properties: {
		Title: {}
	},
	Events:{
		onSelect:{},
		onDeselect:{}
	},
	Contents:{
		content:{}
	}
});